package com.neuedu.datatype;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.neuedu.bean.TUser;
import com.neuedu.util.SqlUtil;

public class DecimalExample {
   public static void main(String[] args) {
	/*TUser use=new TUser();
	use.setUid(6);
	//18.0默认类型是double，转为BigDecimal类型，需要转换，使用BigDecimal的构造器来实现
	use.setPrice(new BigDecimal(18.9));
	
	int i=updateDecimal(use);
	System.out.println("影响的行数："+i);*/
	   TUser use=selectDecimal(6);
	   System.out.println(use.getPrice());
}
   public static TUser selectDecimal(int uid) {
		TUser use=new TUser();
		Connection conn= null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		
		try {
			conn=SqlUtil.getConn();
			String sql="select price from t_use where u_id=?";
			pstm=conn.prepareStatement(sql);
			pstm.setObject(1, uid);
			rs=pstm.executeQuery();
			while(rs.next()) {
				use.setPrice(rs.getBigDecimal("price"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return use;
	}
   
   public static int updateDecimal(TUser use) {
		int i=-1;
		
		Connection conn=null;
		PreparedStatement pstm=null;
		
		
		try {
			conn=SqlUtil.getConn();
			String sql="update t_use set price=? where u_id=?";
			pstm=conn.prepareStatement(sql);
			pstm.setObject(1, use.getPrice());
			//pstm.setBigDecimal(1, use.getPrice());
			pstm.setObject(2, use.getUid()); 
			
			i=pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			SqlUtil.close(pstm,conn);
		}
		
		
		return i;
	}
}
